package com.webapp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.webapp.mapper.PrdRecommAgentDetailMapper;
import com.webapp.mapper.PrdRecommCustomerDetailMapper;
import com.webapp.mapper.ProductComprehensiveScoreMapper;
import com.webapp.model.*;
import com.webapp.model.dto.FamilyInfo;
import com.webapp.model.dto.QuestionnaireInfoDto;
import com.webapp.model.vo.*;
import com.webapp.service.CalculationModelService;
import com.webapp.service.LogService;
import com.webapp.service.PrdRecommProductDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: Jimmy Li
 * @E-mail: jimmy.c.li@cn.pwc.com
 * @Date: 2020/9/11, 1:21 PM
 * @Description:
 */

@Slf4j
@RestController
@RequestMapping("/dockingModel")
@Api(value="返回产品Controller",tags={"接口返回"})
public class QuestionnaireSurveyController {

    @Autowired
    private CalculationModelService calculationModelService;
    @Autowired
    private PrdRecommCustomerDetailMapper prdRecommCustomerDetailMapper;
    @Autowired
    private LogService logService;
    @Autowired
    ProductComprehensiveScoreMapper productComprehensiveScoreMapper;
//    @Autowired
//    AgentBranchCateMapper agentBranchCateMapper;
    @Autowired
    PrdRecommProductDetailService prdRecommProductDetailService;
    @Autowired
    PrdRecommAgentDetailMapper prdRecommAgentDetailMapper;


    /**
     *
     */


    //这是兜底的
    @ApiOperation(value = "接口返回产品")
    @PostMapping("/dockingPython")
    public  RetResult queryDayLogs(@RequestBody QuestionnaireInfoDto questionnaireInfoDto){
        try{
            RetResult retResult = dockingModel(questionnaireInfoDto);

            return retResult;
        }catch (Exception x){
            log.info("----dd-----");
            ArrayList<Long> xinke = calculationModelService.xinke();

            String serialNumber = questionnaireInfoDto.getSerialNumber();//请求序列号
            QuestionnaireInfoVo questionnaireInfoVo = new QuestionnaireInfoVo();
            questionnaireInfoVo.setSerialNumber(serialNumber);
            ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();
            FamilyPrdVo familyPrdVo = new FamilyPrdVo();
            familyPrdVo.setRelation("本人");
            familyPrdVo.setProductId(xinke);
            familyPrdVos.add(familyPrdVo);
            questionnaireInfoVo.setProductIds(familyPrdVos);

            Log log1 = new Log();
            log1.setSerialNumber(questionnaireInfoDto.getSerialNumber());//请求序列号
            Date date1 = new Date();
            log1.setCreateTime(date1);//时间
            log1.setAgentCode(questionnaireInfoDto.getAgentCode());//代理人code
            log1.setMainCustomerId(questionnaireInfoDto.getMainCustomerId());
            ArrayList<String> strings1 = new ArrayList<>();
            if (questionnaireInfoDto.getFamilyInfo()!=null&&questionnaireInfoDto.getFamilyInfo().size()!=0){
                List<FamilyInfo> familyInfo = questionnaireInfoDto.getFamilyInfo();
                for (FamilyInfo info : familyInfo) {
                    strings1.add(info.getRelation()+":"+info.getCustomerId()+" ");
                }
            }
            if (strings1!=null&&strings1.size()!=0){
                log1.setFamilyInfo(strings1.toString());
            }
            log1.setState(0);
             logService.save(log1);
            Long id = log1.getId();
            Log log = new Log();
            log.setId(id);
            log.setSerialNumber(questionnaireInfoDto.getSerialNumber());//请求序列号
            Date date = new Date();
            log.setCreateTime(date);//时间
            log.setAgentCode(questionnaireInfoDto.getAgentCode());//代理人code
            log.setResponse(xinke.get(0)+","+xinke.get(1)+","+xinke.get(2));//返回
            ArrayList<String> strings = new ArrayList<>();
            if (questionnaireInfoDto.getFamilyInfo()!=null&&questionnaireInfoDto.getFamilyInfo().size()!=0){
                List<FamilyInfo> familyInfo = questionnaireInfoDto.getFamilyInfo();
                for (FamilyInfo info : familyInfo) {
                    strings.add(info.getRelation()+":"+info.getCustomerId()+" ");
                }
            }
            if (strings!=null&&strings.size()!=0){
                log.setFamilyInfo(strings.toString());
            }
            log.setMainCustomerId(questionnaireInfoDto.getMainCustomerId());
            log.setState(1);
            logService.updateById(log);
            return RetResponse.makeOKRsp(questionnaireInfoVo);
        }
    }






    @PostMapping("/dockingPython123")
    public RetResult dockingModel(@RequestBody QuestionnaireInfoDto questionnaireInfoDto){

        Date date = new Date();
        String mainCustomerId = questionnaireInfoDto.getMainCustomerId();


        String agentCode = questionnaireInfoDto.getAgentCode();
        List<FamilyInfo> familyInfo = questionnaireInfoDto.getFamilyInfo();

        HashMap<String, String> kehuidAndGuanxiMap = new HashMap<>();
        if (familyInfo!=null){
            for (FamilyInfo info : familyInfo) {
                kehuidAndGuanxiMap.put(info.getCustomerId(),info.getRelation());
            }
        }
        Log log = new Log();






        ArrayList<Long> xinke = calculationModelService.xinke();

        QuestionnaireInfoVo getbenren = calculationModelService.getbenren(questionnaireInfoDto);
        log.setSerialNumber(getbenren.getSerialNumber());
        log.setCreateTime(date);
        log.setAgentCode(agentCode);
        QuestionnaireInfoVo questionnaireInfoVo = new QuestionnaireInfoVo();


        questionnaireInfoVo.setSerialNumber(getbenren.getSerialNumber());
        ArrayList<FamilyPrdVo> familyPrdVos = new ArrayList<>();

        if ((familyInfo==null||familyInfo.size()==0)){
            List<FamilyPrdVo> productIds = getbenren.getProductIds();
            FamilyPrdVo familyPrdVo = productIds.get(0);
            List<Long> productId = familyPrdVo.getProductId();
            ArrayList<Long> longs = new ArrayList<>();

            ArrayList<Long> zuizhongList = new ArrayList<>();

            if (productId.size()<3){
                longs=xinke;
            }else {
                if (productId.get(0)!=null){
                    longs.add(productId.get(0));
                }
                if (productId.get(1)!=null){
                    longs.add(productId.get(1));
                }
                if (productId.get(2)!=null){
                    longs.add(productId.get(2));
                }
                List<Long> collect = longs.stream().distinct().collect(Collectors.toList());
                if (collect.size()<3){
                    zuizhongList=xinke;
                }else {
                    zuizhongList.add(collect.get(0));
                    zuizhongList.add(collect.get(1));
                    zuizhongList.add(collect.get(2));
                }


            }
            log.setMainCustomerId(mainCustomerId);
            log.setState(0);

            logService.save(log);
            Long id = log.getId();

            log.setResponse(zuizhongList.get(0)+","+zuizhongList.get(1)+","+zuizhongList.get(2));
            log.setId(id);
            log.setState(1);




            logService.updateById(log);

            FamilyPrdVo familyPrdVo1= new FamilyPrdVo();
            familyPrdVo1.setRelation("本人");

            familyPrdVo1.setProductId(zuizhongList);
            familyPrdVos.add(familyPrdVo1);
            questionnaireInfoVo.setProductIds(familyPrdVos);




            return RetResponse.makeOKRsp(questionnaireInfoVo);
        }else {
            ArrayList<String> strings3 = new ArrayList<>();//家庭关系里面所有客户的id和关系
            ArrayList<String> strings4 = new ArrayList<>();
            strings4.add(mainCustomerId);
            for (FamilyInfo info : familyInfo) {
                if (info.getCustomerId() != null || info.getCustomerId() != "") {
                    strings3.add(info.getRelation()+":"+info.getCustomerId()+" ");
                    strings4.add(info.getCustomerId());
                }
            }
            String customerString = strings3.toString();

            System.out.println("------家庭关系开始了----");

            List<Long> benrenChanpinList = getbenren.getProductIds().get(0).getProductId();//本人的三款产品





            if (familyInfo.size()==1){

                ArrayList<Long> longs31 = new ArrayList<>();
                System.out.println("---家庭关系一个人---");
                String customerIdString = questionnaireInfoDto.getFamilyInfo().get(0).getCustomerId();

                if (customerIdString==null||customerIdString.equals("")){
                    FamilyPrdVo familyPrdVo1= new FamilyPrdVo();


                    familyPrdVo1.setRelation("本人");
                    familyPrdVo1.setProductId(benrenChanpinList);

                    familyPrdVos.add(familyPrdVo1);
                    questionnaireInfoVo.setProductIds(familyPrdVos);
                    return RetResponse.makeOKRsp(questionnaireInfoVo);


                }
                List<PrdRecommCustomerDetail> customerDetailList = prdRecommCustomerDetailMapper.selectList(new QueryWrapper<PrdRecommCustomerDetail>()
                        .eq("cust_puid", customerIdString)
                .eq("agt_id",agentCode));
                if (customerDetailList==null||customerDetailList.size()==0){
                    FamilyPrdVo familyPrdVo1= new FamilyPrdVo();
                    Long aLong1 = benrenChanpinList.get(0);
                    Long aLong2 = benrenChanpinList.get(1);
                    Long aLong3 = benrenChanpinList.get(2);


                    String gongsi=null;
                    String qudao =null;
                    //GongsiQudao selectgongsiandqudao = agentBranchCateMapper.selectgongsiandqudao(agentCode);
                    GongsiQudao selectgongsiandqudao = prdRecommAgentDetailMapper.selectgongsiandqudao(questionnaireInfoDto.getAgentCode());

                    if (selectgongsiandqudao!=null){
                        if (selectgongsiandqudao.getAgentCate()!=null){
                            qudao=selectgongsiandqudao.getAgentCate();
                        }
                        if (selectgongsiandqudao.getBranchId()!=null){
                            gongsi=selectgongsiandqudao.getBranchId();
                        }
                    }
                    ArrayList<String> strings = new ArrayList<>();
                    strings.add("长险");
                    ArrayList<String> strings1 = new ArrayList<>();
                    strings1.add("短险");
                    strings1.add("一年期");
                    ArrayList<Long> list1 = prdRecommProductDetailService.tanxingmoban(gongsi, qudao, strings, 2);
                    ArrayList<Long> list2 = prdRecommProductDetailService.tanxingmoban(gongsi, qudao, strings1, 1);
                    if (list1.size()!=0&&list1.get(0)!=null){
                        longs31.add(list1.get(0));
                    }
                    if (list1.size()>1&&list1.get(1)!=null){
                        longs31.add(list1.get(1));
                    }
                    if (list2.size()!=0&&list2.get(0)!=null){
                        longs31.add(list2.get(0));
                    }
                    if (longs31.size()<3){
                        longs31=xinke;
                    }

                    ArrayList<Long> longs = new ArrayList<>();
                    longs.add(aLong1);
                    longs.add(longs31.get(0));
                    longs.add(aLong2);
                    longs.add(longs31.get(1));
                    longs.add(aLong3);
                    longs.add(longs31.get(2));
                    List<Long> collect = longs.stream().distinct().collect(Collectors.toList());

                    ArrayList<Long> longs122 = new ArrayList<>();

                    longs122.add(collect.get(0));
                    longs122.add(collect.get(1));
                    longs122.add(collect.get(2));


                    familyPrdVo1.setRelation("本人");

                    familyPrdVo1.setProductId(longs122);

                    familyPrdVos.add(familyPrdVo1);
                    questionnaireInfoVo.setProductIds(familyPrdVos);

                    log.setMainCustomerId(mainCustomerId);
                    log.setCreateTime(date);
                    log.setAgentCode(agentCode);
                    log.setFamilyInfo(customerString);
                    log.setState(0);

                    logService.save(log);
                    Long id = log.getId();

                    log.setResponse(longs122.get(0)+","+longs122.get(1)+","+longs122.get(2));
                    log.setId(id);
                    log.setState(1);
                    logService.updateById(log);



                    return RetResponse.makeOKRsp(questionnaireInfoVo);
                }
                PrdRecommCustomerDetail customerDetail = customerDetailList.get(0);
                FamilyPrdVo familyPrdVo1= new FamilyPrdVo();
                familyPrdVo1.setRelation("本人");
                ArrayList<Long> longs1 = new ArrayList<>();

                if (customerDetail.getAge()<=18&&(!customerDetail.getIfValidNj()||customerDetail.getIfValidNj()==null)){
                    //假设有<=18岁少年，且没配置教育年金，则第一推荐优先级为<=18岁的教育年金（多个子女一样考虑，优先年龄小的）
                    Long oksaleproductId3 = productComprehensiveScoreMapper.selectProid("年金", agentCode);
                    if (oksaleproductId3!=null){
                        longs1.add(oksaleproductId3);
                    }
                    ArrayList<String> strings1 = new ArrayList<>();//医疗
                    strings1.add("22");strings1.add("23");strings1.add("24");strings1.add("25");
                    Long aLong = productComprehensiveScoreMapper.selectProid("医疗", agentCode);
                    if (aLong!=null){
                        longs1.add(aLong);
                    }
                    ArrayList<String> strings2 = new ArrayList<>();//疾病
                    strings2.add("21");
                    Long aLong2= productComprehensiveScoreMapper.selectProid("疾病", agentCode);
                    if (aLong2!=null){
                        longs1.add(aLong2);
                    }
                    if (longs1.size()<3){
                        longs1=xinke;
                    }


                }else {
                    QuestionnaireInfoDto questionnaireInfoDto1 = new QuestionnaireInfoDto();
                    questionnaireInfoDto1.setMainCustomerId(customerDetailList.get(0).getCustPuid());
                    questionnaireInfoDto1.setSerialNumber("123");//请求序列号
                    questionnaireInfoDto1.setAgentCode(customerDetailList.get(0).getAgtId());
                    questionnaireInfoDto1.setMechanism(questionnaireInfoDto.getMechanism());
                    questionnaireInfoDto1.setChannel(questionnaireInfoDto.getChannel());
                    QuestionnaireInfoVo getbenren1 = calculationModelService.getbenren(questionnaireInfoDto1);
                    FamilyPrdVo familyPrdVo3 = getbenren1.getProductIds().get(0);
                    List<Long> productId = familyPrdVo3.getProductId();
                    if (productId.size()<3){
                        longs1=xinke;
                    }else {
                        longs1.add(productId.get(0));
                        longs1.add(productId.get(1));
                        longs1.add(productId.get(2));

                    }
                }
                ArrayList<Long> longs = new ArrayList<>();

                longs.add(benrenChanpinList.get(0));
                longs.add(longs1.get(0));
                longs.add(benrenChanpinList.get(1));
                longs.add(longs1.get(1));
                longs.add(benrenChanpinList.get(2));
                longs.add(longs1.get(2));
                List<Long> collect = longs.stream().distinct().collect(Collectors.toList());
                ArrayList<Long> longs2 = new ArrayList<>();

                longs2.add(collect.get(0));
                longs2.add(collect.get(1));
                longs2.add(collect.get(2));

                familyPrdVo1.setProductId(longs2);
                familyPrdVos.add(familyPrdVo1);

                questionnaireInfoVo.setProductIds(familyPrdVos);



                log.setFamilyInfo(customerString);
                log.setMainCustomerId(mainCustomerId);
                log.setCreateTime(date);
                log.setAgentCode(agentCode);
                log.setState(0);
                logService.save(log);
                log.setResponse(collect.get(0)+","+collect.get(1)+","+collect.get(2));
                Long id = log.getId();
                log.setId(id);
                log.setState(1);
                boolean b = logService.updateById(log);

                return RetResponse.makeOKRsp(questionnaireInfoVo);
            }else if (familyInfo.size()>1){

                ArrayList<Long> duorenxinke = new ArrayList<>();
                String gongsi=null;
                String qudao=null;
//                GongsiQudao selectgongsiandqudao = agentBranchCateMapper.selectgongsiandqudao(agentCode);
                GongsiQudao selectgongsiandqudao = prdRecommAgentDetailMapper.selectgongsiandqudao(questionnaireInfoDto.getAgentCode());

                if (selectgongsiandqudao!=null){
                    if (selectgongsiandqudao.getAgentCate()!=null){
                        qudao=selectgongsiandqudao.getAgentCate();
                    }
                    if (selectgongsiandqudao.getBranchId()!=null){
                        gongsi=selectgongsiandqudao.getBranchId();
                    }
                }
                ArrayList<String> stringschang = new ArrayList<>();
                stringschang.add("长险");
                ArrayList<String> stringsduan = new ArrayList<>();
                stringsduan.add("短险");
                stringsduan.add("一年期");
                ArrayList<Long> listchang = prdRecommProductDetailService.tanxingmoban(gongsi, qudao, stringschang, 2);
                ArrayList<Long> listduan = prdRecommProductDetailService.tanxingmoban(gongsi, qudao, stringsduan, 1);
                if (listchang.size()!=0&&listchang.get(0)!=null){
                    duorenxinke.add(listchang.get(0));
                }
                if (listchang.size()>1&&listchang.get(1)!=null){
                    duorenxinke.add(listchang.get(1));
                }
                if (listduan.size()!=0&&listduan.get(0)!=null){
                    duorenxinke.add(listduan.get(0));
                }
                if (duorenxinke.size()<3){
                    duorenxinke=xinke;
                }




                System.out.println("---家庭关系多个人-----");
                //家庭关系是多个人开始了
                /**
                 * 21-11-15  开始
                 */
                //1:将familyInfo的对象写入map
                ArrayList<String> peious = new ArrayList<>();
                ArrayList<String> zinvs = new ArrayList<>();
                ArrayList<String> fumus = new ArrayList<>();
                ArrayList<String> qita = new ArrayList<>();


                ArrayList<Long> peiouPrdList = new ArrayList<>();
                ArrayList<Long> zinvPrdList = new ArrayList<>();
                ArrayList<Long> fumuPrdList = new ArrayList<>();
                ArrayList<Long> qitaPrdList = new ArrayList<>();


                for (FamilyInfo info : familyInfo) {
                    if (info.getRelation()!=null&&info.getRelation().equals("配偶")){
                        peious.add(info.getCustomerId());
                    }else if (info.getRelation()!=null&&info.getRelation().equals("子女")){
                        zinvs.add(info.getCustomerId());
                    }else if (info.getRelation()!=null&&info.getRelation().equals("父母")){
                        fumus.add(info.getCustomerId());
                    }else if (info.getRelation()!=null&&info.getRelation().equals("其他")){
                        qita.add(info.getCustomerId());
                    }
                }
                if (peious!=null&&peious.size()!=0){
                    Kehu2 kehu2 = prdRecommCustomerDetailMapper.selectlist2(peious,agentCode);
                    if (kehu2==null){
                        peiouPrdList=duorenxinke;

                    }else {
                        QuestionnaireInfoDto questionnaireInfoDto1 = new QuestionnaireInfoDto();
                        questionnaireInfoDto1.setMainCustomerId(kehu2.getCustPuid());
                        questionnaireInfoDto1.setSerialNumber("12");
                        questionnaireInfoDto1.setAgentCode(agentCode);
                        questionnaireInfoDto1.setMechanism(questionnaireInfoDto.getMechanism());
                        questionnaireInfoDto1.setChannel(questionnaireInfoDto.getChannel());
                        questionnaireInfoDto1.setMechanism(questionnaireInfoDto.getMechanism());
                        questionnaireInfoDto1.setChannel(questionnaireInfoDto.getChannel());
                        QuestionnaireInfoVo questionnaireInfoVo1 = calculationModelService.getbenren(questionnaireInfoDto1);
                        FamilyPrdVo familyPrdVo = questionnaireInfoVo1.getProductIds().get(0);
                        List<Long> productId = familyPrdVo.getProductId();
                        if (productId.size()<3){
                            peiouPrdList=xinke;
                        }else {
                            peiouPrdList.add(productId.get(0));
                            peiouPrdList.add(productId.get(1));
                            peiouPrdList.add(productId.get(2));
                        }
                    }


                }
                ArrayList<Long> zinvyoumeiyounianjinList = new ArrayList<>();

                if (zinvs!=null&&zinvs.size()!=0){
                    Kehu2 selectlistzinv = prdRecommCustomerDetailMapper.selectlistzinv(zinvs,agentCode);

                    if (selectlistzinv==null){
                        zinvPrdList=duorenxinke;
                    }else {
//                        if (selectlistzinv.getYesorno().equals("no")){
//                            //没有年金
//                            Long oksaleproductId3 = productComprehensiveScoreMapper.selectProid("年金", agentCode);
//                            zinvyoumeiyounianjinList.add(oksaleproductId3);
//                        }
                        String customerId = selectlistzinv.getCustPuid();
                        QuestionnaireInfoDto questionnaireInfoDto1 = new QuestionnaireInfoDto();
                        questionnaireInfoDto1.setMainCustomerId(customerId);
                        questionnaireInfoDto1.setSerialNumber("12");
                        questionnaireInfoDto1.setAgentCode(agentCode);
                        questionnaireInfoDto1.setMechanism(questionnaireInfoDto.getMechanism());
                        questionnaireInfoDto1.setChannel(questionnaireInfoDto.getChannel());
                        QuestionnaireInfoVo questionnaireInfoVo1 = calculationModelService.getbenren(questionnaireInfoDto1);
                        FamilyPrdVo familyPrdVo = questionnaireInfoVo1.getProductIds().get(0);


                        List<Long> productId = familyPrdVo.getProductId();
                        if (productId.size()<3){
                            zinvPrdList=xinke;
                        }else {
                            zinvPrdList.add(productId.get(0));
                            zinvPrdList.add(productId.get(1));
                            zinvPrdList.add(productId.get(2));
                        }
                    }


                }

                if (fumus!=null&&fumus.size()!=0){
                    Kehu2 kehu2 = prdRecommCustomerDetailMapper.selectlist2(fumus,agentCode);
                    if (kehu2==null){
                        fumuPrdList=duorenxinke;

                    }else {
                        QuestionnaireInfoDto questionnaireInfoDto1 = new QuestionnaireInfoDto();
                        questionnaireInfoDto1.setMainCustomerId(kehu2.getCustPuid());
                        questionnaireInfoDto1.setSerialNumber("12");
                        questionnaireInfoDto1.setAgentCode(agentCode);
                        questionnaireInfoDto1.setMechanism(questionnaireInfoDto.getMechanism());
                        questionnaireInfoDto1.setChannel(questionnaireInfoDto.getChannel());
                        QuestionnaireInfoVo questionnaireInfoVo1 = calculationModelService.getbenren(questionnaireInfoDto1);
                        FamilyPrdVo familyPrdVo = questionnaireInfoVo1.getProductIds().get(0);

                        List<Long> productId = familyPrdVo.getProductId();
                        if (productId.size()<3){
                            fumuPrdList=xinke;
                        }else {
                            fumuPrdList.add(productId.get(0));
                            fumuPrdList.add(productId.get(1));
                            fumuPrdList.add(productId.get(2));

                        }


                    }



                    //
                }
                if (qita!=null&&qita.size()!=0){

                    Kehu2 kehu2 = prdRecommCustomerDetailMapper.selectlist2(qita,agentCode);
                    if (kehu2==null){


                        qitaPrdList=duorenxinke;


                    }else {
                        String customerId = kehu2.getCustPuid();
                        QuestionnaireInfoDto questionnaireInfoDto1 = new QuestionnaireInfoDto();
                        questionnaireInfoDto1.setMainCustomerId(customerId);
                        questionnaireInfoDto1.setSerialNumber("12");
                        questionnaireInfoDto1.setAgentCode(agentCode);
                        questionnaireInfoDto1.setMechanism(questionnaireInfoDto.getMechanism());
                        questionnaireInfoDto1.setChannel(questionnaireInfoDto.getChannel());
                        QuestionnaireInfoVo questionnaireInfoVo1 = calculationModelService.getbenren(questionnaireInfoDto1);
                        FamilyPrdVo familyPrdVo = questionnaireInfoVo1.getProductIds().get(0);


                        List<Long> productId = familyPrdVo.getProductId();
                        if (productId.size()<3){
                            qitaPrdList=xinke;
                        }else {
                            qitaPrdList.add(productId.get(0));
                            qitaPrdList.add(productId.get(1));
                            qitaPrdList.add(productId.get(2));
                        }
                    }
                }



                ArrayList<Long> buchongfuPrdList = new ArrayList<>();
//                if (zinvyoumeiyounianjinList!=null&&zinvyoumeiyounianjinList.size()==1){
//                    buchongfuPrdList.add(zinvyoumeiyounianjinList.get(0));
//                }

                ArrayList<String> strings = new ArrayList<>();
                for (FamilyInfo info : familyInfo) {
                    strings.add(info.getRelation());
                }

                ArrayList<Long> list = new ArrayList<>();
                ArrayList<Long> list1 = new ArrayList<>();
                ArrayList<String> s1 = new ArrayList<>();
                List<String> collect2 = strings.stream().distinct().collect(Collectors.toList());
                if (collect2.size()>2){
                    for (String s : collect2) {
                        if ("配偶".equals(s)){
                            s1.add("配偶");
                        }else if ("子女".equals(s)){
                            s1.add("子女");
                        }else if ("父母".equals(s)){
                            s1.add("父母");
                        }else if ("其他".equals(s)){
                            s1.add("其他");
                        }
                    }
                    ArrayList<String> strings1 = new ArrayList<>();
                    if (s1.contains("配偶")){
                        strings1.add("配偶");
                    } if (s1.contains("子女")){
                        strings1.add("子女");
                    } if (s1.contains("父母")){
                        strings1.add("父母");
                    } if (s1.contains("其他")){
                        strings1.add("其他");
                    }


                    String s = strings1.get(0);//关系
                    String s2 = strings1.get(1);//关系

                    if (s.equals("配偶")&&peiouPrdList.size()!=0){
                        list.addAll(peiouPrdList);
                    } if (s.equals("子女")&&zinvPrdList.size()!=0){
                        list.addAll(zinvPrdList);
                    } if (s.equals("父母")&&fumuPrdList.size()!=0){
                        list.addAll(fumuPrdList);
                    } if (s.equals("其他")&&qitaPrdList.size()!=0){
                        list.addAll(qitaPrdList);
                    }

                    if (s2.equals("配偶")&&peiouPrdList.size()!=0){
                        list1.addAll(peiouPrdList);
                    } if (s2.equals("子女")&&zinvPrdList.size()!=0){
                        list1.addAll(zinvPrdList);
                    } if (s2.equals("父母")&&fumuPrdList.size()!=0){
                        list1.addAll(fumuPrdList);
                    } if (s2.equals("其他")&&qitaPrdList.size()!=0){
                        list1.addAll(qitaPrdList);
                    }
                    buchongfuPrdList.add(benrenChanpinList.get(0));
                    buchongfuPrdList.add(list.get(0));
                    buchongfuPrdList.add(list1.get(0));

                    buchongfuPrdList.add(benrenChanpinList.get(1));
                    buchongfuPrdList.add(list.get(1));
                    buchongfuPrdList.add(list1.get(1));

                    buchongfuPrdList.add(benrenChanpinList.get(2));
                    buchongfuPrdList.add(list.get(2));
                    buchongfuPrdList.add(list1.get(2));

                    List<Long> collect = buchongfuPrdList.stream().distinct().collect(Collectors.toList());
                    ArrayList<Long> list2 = new ArrayList<>();
                    list2.add(collect.get(0));list2.add(collect.get(1));list2.add(collect.get(2));

                    ArrayList<FamilyPrdVo> familyPrdVos1 = new ArrayList<>();

                    FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                    familyPrdVo.setRelation("本人");
                    familyPrdVo.setProductId(list2);
                    familyPrdVos1.add(familyPrdVo);
                    questionnaireInfoVo.setProductIds(familyPrdVos1);


                    Log log1 = new Log();
                    log1.setSerialNumber(questionnaireInfoVo.getSerialNumber());

                    log1.setMainCustomerId(mainCustomerId);
                    log1.setFamilyInfo(customerString);
                    log1.setCreateTime(date);
                    log1.setAgentCode(agentCode);
                    log1.setState(0);
                    logService.save(log1);
                    Long id = log1.getId();
                    log1.setId(id);
                    log1.setState(1);
                    log1.setResponse(collect.get(0)+","+collect.get(1)+","+collect.get(2));
                    logService.updateById(log1);

                    return RetResponse.makeOKRsp(questionnaireInfoVo);


                }




                buchongfuPrdList.add(benrenChanpinList.get(0));
                if (peiouPrdList!=null&&peiouPrdList.size()!=0){
                    buchongfuPrdList.add(peiouPrdList.get(0));
                }
                if (zinvPrdList!=null&&zinvPrdList.size()!=0){
                    buchongfuPrdList.add(zinvPrdList.get(0));
                }
                if (fumuPrdList!=null&&fumuPrdList.size()!=0){
                    buchongfuPrdList.add(fumuPrdList.get(0));
                }
                if (qitaPrdList!=null&&qitaPrdList.size()!=0){
                    buchongfuPrdList.add(qitaPrdList.get(0));
                }
                buchongfuPrdList.add(benrenChanpinList.get(1));
                if (peiouPrdList!=null&&peiouPrdList.size()!=0){
                    buchongfuPrdList.add(peiouPrdList.get(1));
                }
                if (zinvPrdList!=null&&zinvPrdList.size()!=0){
                    buchongfuPrdList.add(zinvPrdList.get(1));
                }
                if (fumuPrdList!=null&&fumuPrdList.size()!=0){
                    buchongfuPrdList.add(fumuPrdList.get(1));
                }
                if (qitaPrdList!=null&&qitaPrdList.size()!=0){
                    buchongfuPrdList.add(qitaPrdList.get(1));
                }

                buchongfuPrdList.add(benrenChanpinList.get(2));
                if (peiouPrdList!=null&&peiouPrdList.size()!=0){
                    buchongfuPrdList.add(peiouPrdList.get(2));
                }
                if (zinvPrdList!=null&&zinvPrdList.size()!=0){
                    buchongfuPrdList.add(zinvPrdList.get(2));
                }
                if (fumuPrdList!=null&&fumuPrdList.size()!=0){
                    buchongfuPrdList.add(fumuPrdList.get(2));
                }
                if (qitaPrdList!=null&&qitaPrdList.size()!=0){
                    buchongfuPrdList.add(qitaPrdList.get(2));
                }

                System.out.println(buchongfuPrdList);
                List<Long> collect = buchongfuPrdList.stream().distinct().collect(Collectors.toList());
                ArrayList<Long> longs = new ArrayList<>();
                longs.add(collect.get(0));longs.add(collect.get(1));longs.add(collect.get(2));


                ArrayList<FamilyPrdVo> familyPrdVos1 = new ArrayList<>();

                FamilyPrdVo familyPrdVo = new FamilyPrdVo();
                familyPrdVo.setRelation("本人");
                familyPrdVo.setProductId(longs);
                familyPrdVos1.add(familyPrdVo);
                questionnaireInfoVo.setProductIds(familyPrdVos1);


                Log log1 = new Log();
                log1.setSerialNumber(questionnaireInfoVo.getSerialNumber());

                log1.setMainCustomerId(mainCustomerId);
                log1.setFamilyInfo(customerString);
                log1.setCreateTime(date);
                log1.setAgentCode(agentCode);
                log1.setState(0);
                logService.save(log1);

                log1.setResponse(collect.get(0)+","+collect.get(1)+","+collect.get(2));
                log1.setState(1);
                log1.setId(log1.getId());
                logService.updateById(log1);

                return RetResponse.makeOKRsp(questionnaireInfoVo);
            }
        }

        return RetResponse.makeOKRsp();

    }


//    @ApiOperation(value = "测试一下")
//    @PostMapping("/测试一下")
//    public  GongsiQudao queryDayLogs(){
//        GongsiQudao selectgongsiandqudao = agentBranchCateMapper.selectgongsiandqudao("123");
//return selectgongsiandqudao;
//
//    }


}
